<template>
  <div>
    <h1>会议室管理</h1>
    <el-card class="mt-10" shadow="hover">
      <div class="flex-c" style="flex-wrap: wrap">
        <div>
          <span>会议室名称:</span>
          <el-input v-model="searchParams.name" style="width: 120px" class="ml-10" clearable />
        </div>
        <div class="ml-20">
          <span>容纳人数:</span>
          <el-input v-model="searchParams.capacity" style="width: 120px" class="ml-10" clearable />
        </div>

        <div class="ml-20">
          <span>设备:</span>
          <el-input v-model="searchParams.equipment" style="width: 120px" class="ml-10" clearable />
        </div>
        <el-button type="primary" class="ml-30" @click="querySearch">查询</el-button>
      </div>
    </el-card>
    <el-card class="mt-15" shadow="hover">
      <div class="f-bold f-18">用户列表</div>
      <el-table :data="tableData" stripe style="width: 100%" class="mt-10">
        <el-table-column prop="name" label="名称" width="180" />
        <el-table-column prop="capacity" label="容纳人数" width="180" />
        <el-table-column prop="location" label="位置" />
        <el-table-column prop="equipment" label="设备" />

        <el-table-column prop="description" label="描述" />
        <el-table-column prop="createTime" label="添加时间" />
        <el-table-column prop="updateTime" label="上次更新时间" />
        <el-table-column prop="updateTime" label="预定状态">
          <template #default="scope">
            <el-tag type="danger" v-if="scope.row.isBooked">已被预订</el-tag>
            <el-tag type="success" v-else>可预定</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="action" label="操作" width="180">
          <template #default="scope">
            <div class="flex">
              <el-button type="primary" size="small" @click="handleBooking(scope.row)"
                >预定</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-row justify="end" class="mt-15">
        <el-pagination
          background
          layout="prev, pager, next,total"
          :total="total"
          v-model:page-size="searchParams.pageSize"
          v-model:current-page="searchParams.pageNo"
          @change="changePage"
        />
      </el-row>
    </el-card>

    <CreateBookingModal v-model="isShowModal" @on-ok="handleAddBooking" :modalInfo="modalInfo">
    </CreateBookingModal>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { meetingRoomList, bookingAdd } from '@/api'
import CreateBookingModal, { type CreateBooking } from './CreateBookingModal'
import { ElMessage } from 'element-plus'

defineOptions({
  name: 'meetingRoomManage'
})

export interface SearchMeetingRoom {
  name: string
  capacity: number | string
  equipment: string
  pageNo: number
  pageSize: number
}

export interface IMeetingRoomResult {
  id: number
  name: string
  capacity: number
  equipment: string
  description: string
  location: string

  isBooked: boolean
  createTime: Date
  updateTime: Date
}

const searchParams = reactive<SearchMeetingRoom>({
  name: '',
  capacity: '',
  equipment: '',
  pageNo: 1,
  pageSize: 10
})

let tableData = ref<IMeetingRoomResult | null>(null)
let total = ref(0)
const isShowModal = ref(false)
const modalInfo = reactive({
  meetingRoomId: 0,
  meetingRoomName: ''
})

onMounted(() => {
  search()
})

const search = async () => {
  const res = await meetingRoomList(searchParams)
  if (res.status === 200 || res.status === 201) {
    const { data } = res.data
    tableData.value = data.list || []
    total.value = data.total || 0
  }
}

const querySearch = () => {
  searchParams.pageNo = 1
  search()
}

const changePage = (currentPage: number, pageSize: number) => {
  searchParams.pageNo = currentPage
  searchParams.pageSize = pageSize
  search()
}

const handleBooking = (row: IMeetingRoomResult) => {
  modalInfo.meetingRoomId = row.id
  modalInfo.meetingRoomName = row.name
  isShowModal.value = true
}
const handleAddBooking = async (data: CreateBooking) => {
  const params = {
    meetingRoomName: data.meetingRoomName,
    meetingRoomId: data.meetingRoomId,
    startTime: data.startTime,
    endTime: data.endTime,
    note: data.note
  }
  const res = await bookingAdd(params)
  if (res.status === 200 || res.status === 201) {
    ElMessage.success('预定成功')
    search()
  }
}
</script>

<style scoped></style>
